﻿
using System.Collections.Generic;
using System.Windows.Forms;
using GDFlacTool.Common;

namespace GDFlacTool.Forms
{
    public partial class FormAutoNumber : Form
    {
        private readonly IList<Track> _tracks;
        private readonly int _totalTracks;

        public FormAutoNumber(IList<Track> tracks)
        {
            InitializeComponent();

            chkFormat.Checked = Properties.Settings.Default.UseTrackFormat;
            _tracks = tracks;
            _totalTracks = _tracks.Count;
            PreviewTrackNumber();
        }

        private void btnAutoNumber_Click(object sender, System.EventArgs e)
        {
            Properties.Settings.Default.UseTrackFormat = chkFormat.Checked;

            for(int i = 0; i <_totalTracks; i++)
            {
                if(chkFormat.Checked)
                {
                    _tracks[i].TrackNumber = string.Format("{0:0#}/{1:0#}", i + nudTrackStart.Value, _totalTracks);
                }
                else
                {
                    _tracks[i].TrackNumber = string.Format("{0:0#}", i + nudTrackStart.Value);
                }
            }
        }

        private void PreviewTrackNumber()
        {
            string preview;

            if (chkFormat.Checked)
            {
                preview = string.Format("{0:0#}/{1:0#}", 1, 1);
            }
            else
            {
                preview = string.Format("{0:0#}", 1);
            }

            lblInfo.Text = string.Format("Auto fill track number for all selected files, example: ({0})", preview);
        }

        private void chkFormat_CheckedChanged(object sender, System.EventArgs e)
        {
            PreviewTrackNumber();
        }
    }
}
